// ==UserScript==
// @name         知乎增强
// @namespace    https://gitee.com/bluesword12350/web-script
// @version      1.0.0
// @description  知乎增强：1.自动展开关注列表中被收起的知乎动态，2.已阅读的知乎动态自动变灰
// @author       李林峰
// @match        https://www.zhihu.com/*
// @icon         https://static.zhihu.com/heifetz/favicon.ico
// @updateURL    https://gitee.com/bluesword12350/web-script/raw/master/tampermonkey/sword-zhihu.js
// @downloadURL  https://gitee.com/bluesword12350/web-script/raw/master/tampermonkey/sword-zhihu.js
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(() => {
  'use strict';
  const setOpacity = '0.5'
  const maxQuestions = 3000;
  const maxAnswers = 3000;
  const GmAnswerKey = "知乎已读答案";
  const GmQuestionKey = "知乎已读问题";
  const answerURLPattern = new URLPattern("https://www.zhihu.com/question/:questionId/answer/:answerId");
  const questionURLPattern = new URLPattern("https://www.zhihu.com/question/:questionId");
  const readAnswerIds = GM_getValue(GmAnswerKey, []);
  const readQuestionIds = GM_getValue(GmQuestionKey, []);
  exec();
  function construction() {
    chickFeedGroupCollapsedItems();
    hideFeeds();
    recordAnswerOnClick();
  }
  function saveReadAnswerId(answerId) {
    answerId = answerId.toString()
    let nowReadAnswerIds = GM_getValue(GmAnswerKey, []);
    if (!(nowReadAnswerIds instanceof Array)) {
      GM_setValue(GmAnswerKey, []);
    }
    if (!nowReadAnswerIds.includes(answerId)) {
      nowReadAnswerIds.unshift(answerId)
      GM_setValue(GmAnswerKey, nowReadAnswerIds);
    }
    while (nowReadAnswerIds.length > maxAnswers) {
      nowReadAnswerIds.pop()
    }
  }
  function recordAnswerOnClick(){
    let answerItem = document.getElementsByClassName('AnswerItem')
    for (let answerItemElement of answerItem) {
      answerItemElement.onclick = event => {
        if (event.target.classList.contains('ContentItem-more')) {
          let answerId = JSON.parse(answerItemElement.getAttribute('data-zop'))?.itemId;
          saveReadAnswerId(answerId)
        }
      }
    }
  }
  function chickFeedGroupCollapsedItems() {
    let feedGroupCollapsedItems = document.getElementsByClassName('Topstory-feedGroupCollapsedItem')
    for (let feedGroupCollapsedItem of feedGroupCollapsedItems) {
      feedGroupCollapsedItem.click()
    }
  }
  function hideFeeds() {
    let feeds = document.getElementsByClassName('Feed')
    for (let feed of feeds) {
      if (feed.style.opacity === setOpacity) {
        continue;
      }
      let content = JSON.parse(feed.getAttribute("data-za-extra-module"))?.card?.content;
      if (content?.type === "Answer" && readAnswerIds.includes(content?.token)) {
        feed.style.opacity = setOpacity
      }
    }
    let hotItems = document.getElementsByClassName('HotItem-content')
    for (let hotItem of hotItems) {
      let questionURLPatternResult = questionURLPattern.exec(hotItem.getElementsByTagName('a')[0].href)
      if (questionURLPatternResult && readQuestionIds.includes(questionURLPatternResult.pathname.groups.questionId)) {
        hotItem.style.opacity = setOpacity
      }
    }
  }
  function exec(){
    let content = document.getElementById('TopstoryContent')
    if (content) {
      new MutationObserver(construction).observe(content,{attributes: true, childList: true, subtree: true});
    }
    construction();
    {
      let answerURLPatternResult = answerURLPattern.exec(window.location.href)
      if (answerURLPatternResult) {
        let answerId = answerURLPatternResult.pathname.groups.answerId;
        saveReadAnswerId(GmAnswerKey, answerId);
      }
    }
    {
      let questionURLPatternResult = questionURLPattern.exec(window.location.href)
      if (questionURLPatternResult) {
        let questionId = questionURLPatternResult.pathname.groups.questionId;
        let nowReadQuestionIds = GM_getValue(GmQuestionKey, []);
        if (!(nowReadQuestionIds instanceof Array)){
          GM_setValue(GmQuestionKey, []);
        }
        if (!nowReadQuestionIds.includes(questionId)) {
          nowReadQuestionIds.unshift(questionId)
          GM_setValue(GmQuestionKey, nowReadQuestionIds);
        }
        while (nowReadQuestionIds.length > maxQuestions) {
          nowReadQuestionIds.pop()
        }
      }
    }
  }
})();
